package com.cyh.pangu.offer;

import java.util.PriorityQueue;
import java.util.Queue;

public class Offer40 {

    public int[] getLeastNumbers(int[] arr, int k) {
        if (k <= 0) {
            return new int[0];
        }
        Queue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
        for (int num :  arr) {
            if (queue.size() < k) {
                queue.add(num);
            } else if (queue.element() > num) {
                queue.poll();
                queue.offer(num);
            }
        }
        int[] res = new int[queue.size()];
        int idx = 0;
        for (int num : queue) {
            res[idx++] = num;
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = {0,0,0,2,0,5};
        Offer40 offer40 = new Offer40();
        int[] result = offer40.getLeastNumbers(arr, 0);
        for (int cc : result) {
            System.out.println("cc:" + cc);
        }
    }
}
